package com.h3bpm.handler;

import OThinker.Common.Organization.Models.User;
import OThinker.H3.Entity.EventHandlers.IWorkItemEventHandler;
import OThinker.H3.Entity.IEngine;
import OThinker.H3.Entity.WorkItem.ActionEventType;
import OThinker.H3.Entity.WorkItem.WorkItemModels.WorkItem;
import OThinker.H3.Entity.WorkItem.WorkItemState;
import com.h3bpm.web.msg.service.TblSmsService;
import data.DataRow;
import data.DataTable;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;

import java.util.List;

/** 工作任务触发事件
 * Created by wjq on 2019/12/12.
 */
public class TaskWorkItemHandler implements IWorkItemEventHandler {
    private static final Logger logger = LoggerFactory.getLogger(TaskWorkItemHandler.class);

    // 创建工作任务事件
    @Override
    public void OnCreated(IEngine engine, WorkItem item) {
        try {
            String receiver = ((User) engine.getOrganization().GetUnit(item.getParticipant())).getCode();

            logger.info("《》《》《》@待办 OnCreated WorkItem WorkItemID={}, State={}", item.getWorkItemID(), item.getState());

            // 第一个手工节点
            if (1 == item.getTokenId()){
                //或者结束节点
            }else if(WorkItemState.Finished.equals(item.getState())){
            }else if (WorkItemState.Canceled.equals(item.getState())){
            } else{
                logger.info("《》@待办 转办  代办 OnCreated WorkItem receiver={}", receiver);
                 UserProtalHelper.createWorkItem(engine, item);
                if(item.getPreActionEventType()== ActionEventType.Backward){
                    //驳回产生的待办添加短信信息
                    OrcaleSelectHelper.backwardSendMessage(engine, item);
                }
            }
            PushToPortal.pushItemToPortal(engine, item, receiver);
        } catch (Exception e) {
            e.printStackTrace();
        }
}
    // 工作任务更新事件
    @Override
    public void OnUpdated(IEngine engine, WorkItem workItem) {
        try {
            String receiver = ((User) engine.getOrganization().GetUnit(workItem.getParticipant())).getCode();
            logger.info("《》《》《》@待办 OnUpdated WorkItem WorkItemID={}, State={}", workItem.getWorkItemID(), workItem.getState());
            logger.info("《》《》《》@待办 OnUpdated WorkItem receiver={}",receiver );
            boolean urged = workItem.getUrged();
            //发生移交任务事件
            logger.info(">>>>>>工作任务更新@Update WorkItem id={}, state={}", workItem.getWorkItemID(), workItem.getState());
            UserProtalHelper.createWorkItem(engine, workItem);
            //已办推送
            PushToPortal.pushItemToPortal(engine, workItem, receiver);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    //任务移除事件
    @Override
    public void OnRemoved(IEngine engine, String ObjectID) throws Exception {
        logger.info("任务移除事件 待办 OnRemoved==========String ObjectID=========="+ObjectID);
        String selectSql = "select Participant,WorkflowCode from ot_workitem where ObjectID = '%s'";
        DataTable table = engine.getQuery().QueryTable(String.format(selectSql, ObjectID));
        List<DataRow> entityRows = table.getEntityRows();
        if(!CollectionUtils.isEmpty(entityRows)){
            String participant = entityRows.get(0).getString("Participant");
            String workflowCode = entityRows.get(0).getString("WorkflowCode");
            //已办推送
            PushToPortal.pushFinishToPortal(engine, "1", ObjectID, participant, WorkItemState.Finished, workflowCode);
        }
    }
}
